#include <fstream>
#include <iostream>
#include "u1.h"
#include "symmetric_matrix.h"

using namespace std;

/*
 * Take outputs from bellkor_Abar and produce \hat(A) from
 * Bell, Koren. (2007). Improved Neighbourhood-based Collaborative Filtering.
 * (kdd-2007-cf-workshop on their website)
 *
 * Takes:
 * 1. File name of Abar symmetric matrix (in binary).
 * 2. File name of U symmetric matrix (in binary).
 * 3. Beta parameter.
 */
int main(int argc, char **argv) {
  symmetric_matrix<float> Abar(NUM_MOVIES);
  symmetric_matrix<float> U(NUM_MOVIES);
  symmetric_matrix<float> Ahat(NUM_MOVIES);

  if (argc != 4) {
    TRACE<<"see comments for usage"<<endl;
    exit(EXIT_FAILURE);
  }
  string Abar_file = argv[1];
  string U_file = argv[2];
  float beta = (float)atof(argv[3]);
  TRACE<<"Reading Abar from "<<Abar_file<<endl;
  TRACE<<"Reading U from "<<U_file<<endl;

  ifstream Abar_in(Abar_file.c_str());
  Abar.read_binary(Abar_in);
  Abar_in.close();

  ifstream U_in(U_file.c_str());
  U.read_binary(U_in);
  U_in.close();

  TRACE<<"Read inputs. Taking averages over Abar."<<endl;

  // As suggested in paper, take separate averages for diagonal and nondiagonal.
  // Here I sum up all entries (upper and lower triangle), but that's not
  // necessary.
  double Abar_diag = 0;
  double Abar_nondiag = 0;
  size_t n = Abar.size();
  for (size_t i = 0; i < n; ++i) {
    for (size_t j = 0; j < n; ++j) {
      if (i == j)
        Abar_diag += Abar(i,j);
      else
        Abar_nondiag += Abar(i,j);
    }
  }
  TRACE<<"Abar diagonal total: "<<Abar_diag<<endl;
  TRACE<<"Abar nondiagonal total: "<<Abar_nondiag<<endl;
  float Abar_diag_avg = (float)(Abar_diag / n);
  float Abar_nondiag_avg = (float)(Abar_nondiag / (n*n - n));
  TRACE<<"Abar diagonal mean: "<<Abar_diag_avg<<endl;
  TRACE<<"Abar nondiagonal mean: "<<Abar_nondiag_avg<<endl;

  for (size_t i = 0; i < n; ++i) {
    for (size_t j = i; j < n; ++j) {
      float avg = i == j ? Abar_diag_avg : Abar_nondiag_avg;
      Ahat(i,j) = (U(i,j)*Abar(i,j) + beta*avg) / (U(i,j) + beta);
    }
  }

  TRACE<<"Computed Ahat. Writing."<<endl;

  Ahat.write_binary(cout);

  TRACE<<"done"<<endl;

  return 0;
}
/*
* Copyright (c) 2009 John Lees-Miller
* 
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
* 
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/

